package cn.vconinfo.lims.delivery.service;

import cn.vconinfo.database.annotation.TargetDataSource;
import cn.vconinfo.lims.delivery.entity.CollectionDTO;
import cn.vconinfo.lims.delivery.entity.ProjectDTO;
import cn.vconinfo.lims.delivery.entity.SpecimenRegisterQuery;
import cn.vconinfo.lims.delivery.entity.UserDTO;
import cn.vconinfo.lims.delivery.mapper.CustomConfigMapper;
import cn.vconinfo.lims.delivery.mapper.SpecimenRegisterMapper;
import cn.vconinfo.lims.delivery.mapper.SpecimenRegisterResultMapper;
import cn.vconinfo.lims.delivery.model.SpecimenRegister;
import cn.vconinfo.lims.delivery.model.SpecimenRegisterExample;
import cn.vconinfo.lims.delivery.model.SpecimenRegisterResult;
import cn.vconinfo.lims.delivery.model.SpecimenRegisterResultExample;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TestItemSynchroService {

    private final SpecimenRegisterMapper specimenRegisterMapper;
    private final SpecimenRegisterResultMapper specimenRegisterResultMapper;

    private final CustomConfigMapper customConfigMapper;

    /**
     *  根据条码号获取登记数据
     *
     * @param query
     * @return
     */
    @TargetDataSource("master")
    public List<SpecimenRegister> querySpecimenRegister(SpecimenRegisterQuery query){
        SpecimenRegisterExample example = new SpecimenRegisterExample();
        SpecimenRegisterExample.Criteria criteria = example.createCriteria();
        criteria.andHospitalIdEqualTo(query.getHospitalId());
        criteria.andPlaceholderCodeEqualTo(query.getPlaceholderCode());
        criteria.andStatusEqualTo(query.getStatus());
        criteria.andSpecimenStatusGreaterThan(query.getSpecimenStatusBig());
        List<SpecimenRegister> registerList = specimenRegisterMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(registerList)){
            registerList = Collections.emptyList();
        }

        return registerList;
    }

    /**
     *  根据样本条码获取结果
     *
     * @param query
     * @return
     */
    @TargetDataSource("master")
    public List<SpecimenRegisterResult> querySpecimenRegisterResult(SpecimenRegisterQuery query){
        SpecimenRegisterResultExample example = new SpecimenRegisterResultExample();
        SpecimenRegisterResultExample.Criteria criteria = example.createCriteria();
        criteria.andApplicationLinkIdEqualTo(query.getSpecimenExtraId());

        List<SpecimenRegisterResult> registerResultList = specimenRegisterResultMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(registerResultList)){
            registerResultList = Collections.emptyList();
        }

        return registerResultList;
    }

    /**
     *  根据条件获取项目信息
     *
     * @param projectIdList
     * @return
     */
    @TargetDataSource("master")
    public List<ProjectDTO> querySpecimenProject(List<Long> projectIdList){

        List<ProjectDTO> projectDTOList = customConfigMapper.queryProjectList(projectIdList);
        if(CollectionUtils.isEmpty(projectDTOList)){
            projectDTOList = Collections.emptyList();
        }

        return projectDTOList;
    }

    @TargetDataSource("master")
    public Map<Long, ProjectDTO> getProjectMap(List<Long> projectIdList){
        List<ProjectDTO> projectDTOList = querySpecimenProject(projectIdList);
        Map<Long, ProjectDTO> projectDTOMap = projectDTOList.parallelStream().collect(Collectors.toMap(ProjectDTO::getProjectId, a -> a, (k1, k2) -> k1));
        if(projectDTOMap == null || projectDTOMap.isEmpty()){
            projectDTOMap = Collections.emptyMap();
        }

        return projectDTOMap;
    }

    /**
     *  检验项目的单位
     *
     * @return
     */
    @TargetDataSource("master")
    public Map<Long, CollectionDTO> getTestItemUnit(){
        List<CollectionDTO> collection = customConfigMapper.getCollection();
        Map<Long, CollectionDTO> collect = collection.parallelStream().collect(Collectors.toMap(CollectionDTO::getCollectionId, a -> a, (k1, k2) -> k1));
        return collect;
    }

    /**
     *  获取检验用户
     *
     * @param userId
     * @return
     */
    @TargetDataSource("master")
    public UserDTO getSpecimenUser(Long userId){
        UserDTO infoById = customConfigMapper.getUserInfoById(userId);
        return infoById;
    }

}
